# encoding=utf-8
# author:梁子龙
from flask_restful import Resource
from flask import current_app, request, g
from asys_main.resource.ariticle.serializer import ArticleList
from comment.modules.article import Article

from comment.utils.blog_redis import redis_client
from flask_restful.reqparse import RequestParser
from comment.modules import db
from comment.utils.tokens import generate_tokens, verify_tokens
from comment.utils.decorators import login_required
from asys_main.resource.ariticle.serializer import ArticleList


class ArticleInfo(Resource):

    def get(self):
        datas = Article.query.all()  # list
        data = ArticleList(datas).to_dict()
        return {"msg": "查询成功", "data": data}

    def post(self):
        user_id = g.user_id
        rp = RequestParser()
        rp.add_argument('article_title', required=True)
        rp.add_argument('article_content', required=True)
        args = rp.parse_args()
        article_title = args.article_title
        article_content = args.article_content
        a = Article(article_user_id=user_id, article_title=article_title, article_content=article_content)
        db.session.add(a)
        db.session.commit()
        return {"msg": "新增成功", "data": [], "code": "20001"}
        #
        # u = User.query.filter(User.username == username).first()
        # if u:  # 用户名存在
        #     return {'message': '用户名重复', 'code': 20001}

    def put(self):
        rp = RequestParser()
        rp.add_argument('id', required=True)
        rp.add_argument('article_title', required=True)
        rp.add_argument('article_content', required=True)
        args = rp.parse_args()
        id = int(args.id)
        article_title = args.article_title
        article_content = args.article_content
        article = Article.query.filter(Article.id == id).first()
        article.article_title = article_title
        article.article_content = article_content
        db.session.commit()
        return {"msg": "修改成功", "data": "ok"}

    def delete(self):#405
        rp = RequestParser()
        rp.add_argument('id', required=True)
        args = rp.parse_args()
        id = int(args.id)

        article = Article.query.filter(Article.id == id).first()
        db.session.delete(article)
        db.session.commit()
        return {"msg": "修改成功", "data": "ok"}

class ArticleView(Resource):
    def get(self, id):
        article_id = id
        article_view = Article.query.filter(Article.id == article_id).first()
        view = article_view.article_view_count
        article_view.article_view_count = view + 1
        db.session.commit()
        return {"msg": "浏览成功", "data": "ls"}


class ArticleLike(Resource):
    # todo 未实现
    def get(self, id):
        article_id = id
        article_view = Article.query.filter(Article.id == article_id).first()
        if article_view.article_like_count:
            like = article_view.article_like_count

            if not g.flag:
                article_view.article_like_count = like + 1
                g.flag = True
                db.session.commit()
            else:
                article_view.article_like_count = like - 1
                db.session.commit()
        else:
            article_view.article_like_count = 1
            db.session.commit()

        return {"msg": "点赞成功", "data": "ls"}
